/**
 * Collaborative Story Development Web Application (CSDApp) 
 * Copyright Anton Strack 2014
 *
 * This file is part of Collaborative Story Development Application (CSDApp).
 *
 * CSDApp is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * CSDApp is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CSDApp. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package csdwa;

import java.sql.Connection;

/**
 * This class provides methods for selecting the proper relationship mapper dao
 * as well as for sending relationships of a particular resource post to the
 * request object.
 *
 * @author Anton Strack
 */
public class RelationshipManager {

    private Connection conn;

    public RelationshipManager(Connection conn) {
        this.conn = conn;
    }

    /**
     * This method returns the proper relatedMapperDao according to the
     * relationship resourceId.
     *
     * @param relationshipResourceId
     * @return The related mapper dao of the specific relationship type
     */
    public RelatedMapperDao getRelatedMapperDao(int relationshipResourceId) {
        // first determine the relationship type.
        InfoAggregator infoAggregator = InfoAggregator.getInstance();
        if (infoAggregator.getResourceTypeName(relationshipResourceId).equalsIgnoreCase("characterdialog")) {
            return new RelatedMapperDao(conn, infoAggregator.getResourceTypeId("character"), infoAggregator.getResourceTypeId("dialog"));
        } else if (infoAggregator.getResourceTypeName(relationshipResourceId).equalsIgnoreCase("characterrelationship")) {
            return new RelatedMapperDao(conn, infoAggregator.getResourceTypeId("character"), infoAggregator.getResourceTypeId("character"));
        } else if (infoAggregator.getResourceTypeName(relationshipResourceId).equalsIgnoreCase("characterinplot")) {
            return new RelatedMapperDao(conn, infoAggregator.getResourceTypeId("character"), infoAggregator.getResourceTypeId("plot"));
        } else if (infoAggregator.getResourceTypeName(relationshipResourceId).equalsIgnoreCase("plotdialog")) {
            return new RelatedMapperDao(conn, infoAggregator.getResourceTypeId("dialog"), infoAggregator.getResourceTypeId("plot"));
        } else if (infoAggregator.getResourceTypeName(relationshipResourceId).equalsIgnoreCase("plothierarchy")) {
            return new RelatedMapperDao(conn, infoAggregator.getResourceTypeId("plot"), infoAggregator.getResourceTypeId("plot"));
        } else if (infoAggregator.getResourceTypeName(relationshipResourceId).equalsIgnoreCase("plotlocation")) {
            return new RelatedMapperDao(conn, infoAggregator.getResourceTypeId("location"), infoAggregator.getResourceTypeId("plot"));
        } else {
        }
        return null;//rmDao;
    }

    /**
     * This method attaches the posts that make up the column B section of the
     * relationship to the request object for the request bean stored in the
     * request object as "bean"
     *
     * @param request the request object containing the bean that represents
     * postA of the relationship.
     * @param relationshipResourceId the base type of the relationship
     * @return the request object filled with any related B posts.
     */
    public CsdwaRequest attachPostBRelationshipsToPost(CsdwaRequest request, int relationshipResourceId) {
        RelatedMapperDao rmDao = this.getRelatedMapperDao(relationshipResourceId);
        if (rmDao != null && request.getBean() != null) {
            Post post = (Post) request.getBean();
            String attributeName = InfoAggregator.getInstance().getResourceTypeName(relationshipResourceId);
            rmDao.selectRelatedBPostsByAPostId(post.getId(), true);
            request.setAttribute(attributeName, rmDao.getPostBDao().getBeans());
        }
        return request;
    }

    /**
     * This method attaches the posts that make up the column A section of the
     * relationship to the request object for the request bean stored in the
     * request object as "bean"
     *
     * @param request the request object containing the bean that represents
     * postB of the relationship.
     * @param relationshipResourceId the base type of the relationship
     * @return the request object filled with any related A posts.
     */
    public CsdwaRequest attachPostARelationshipsToPost(CsdwaRequest request, int relationshipResourceId) {
        RelatedMapperDao rmDao = this.getRelatedMapperDao(relationshipResourceId);
        if (rmDao != null && request.getBean() != null) {
            Post post = (Post) request.getBean();
            String attributeName = InfoAggregator.getInstance().getResourceTypeName(relationshipResourceId);
            rmDao.selectRelatedAPostsByBPostId(post.getId(), true);
            request.setAttribute(attributeName, rmDao.getPostBDao().getBeans());
        }
        return request;
    }

    /**
     * This method retrieves the mapping ids from the specified relationship
     * table. The RelatedMap object that this method attaches to the request
     * contains the A and B column ids, the relatinship type between the two.
     * Additionally it contains derived values of the resource type ids for A
     * and B as well as the names of A and B. This method properly selects all
     * mappings for the particular resource.
     *
     * @param request the request object containing the bean whose id is used to
     * select the mappings.
     * @param relationshipResourceId the base type of the relationship
     * @return the request object filled with all the relatinships for the bean
     * stored in the request object.
     */
    public CsdwaRequest attachPostMappingsToPost(CsdwaRequest request, int relationshipResourceId) {
        RelatedMapperDao rmDao = this.getRelatedMapperDao(relationshipResourceId);
        if (rmDao != null && request.getBean() != null) {

            Post post = (Post) request.getBean();
            String attributeName = InfoAggregator.getInstance().getResourceTypeName(relationshipResourceId);
            rmDao.setUseRelatedMarkers(true);
            // need to determine which mapping to select by.
            int primaryTypeId = post.getResourceTypeId();
            if (primaryTypeId == rmDao.getRelatedAResourceTypeId()) {
                int result = rmDao.selectMappingsByPostAId(post.getId());
            }
            if (primaryTypeId == rmDao.getRelatedBResourceTypeId()) {
                int result = rmDao.selectMappingsByPostBId(post.getId());
            }
            request.setAttribute(attributeName, rmDao.getBeans());
        }
        return request;
    }
}
